﻿package net.area80.file {
	import flash.events.Event;
	import flash.net.FileFilter;
	
	import mdm.*;
	/**
	 * MDMImageBrowser
	 * @author Wissarut Pimanmassuriya
	 */
	public class MDMImageBrowser extends ImageBrowser {
		public function MDMImageBrowser (type:/*FileFilter*/Array, size:int = 0, smoothing:Boolean = false):void {
				super(type, size, smoothing);
				
		}
		private function createFilterListString ():String {
				var str = "";
				for (var i:int = 0; i < type.length; i++) {
						str += type[i].description + "|" + "*." + type[i].extension.split(".")[1];
						if (i < type.length - 1) str += "|";
				}
				return str;
		}
		override public function browse():void {
	
				mdm.Dialogs.BrowseFile.dialogText = "Select a image file.";
				mdm.Dialogs.BrowseFile.filterList = createFilterListString();
				var myFile:String = mdm.Dialogs.BrowseFile.show();
				if(myFile && myFile != "false") {
					var fileSize:Number = mdm.FileSystem.getFileSize(myFile);
					if (size!= 0 && Number(fileSize) > size) {
							dispatchErrorEvent("File size is larger than " + Math.round(size * 0.001) + "kb.");
					} else if (Number(fileSize) == 0) {
							dispatchErrorEvent("File not found");
					} else {
							dispatchEvent(new Event(Event.SELECT));
							//file exist
							loadFile(myFile);
					}
				} else {
					dispatchErrorEvent("No file was selected.");
				}
		}
	
	}
}